<template>
  <a-modal
    :visible="show"
    width="70vw"
    height="60vh"
    :bodyStyle="{ padding: '10px' }"
    title="批量更新报工人"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form-model ref="ruleForm">
      <a-row :gutter="24">
        <a-col :span="24">
          <a-radio-group v-model="form.type" default-value="1" @change="changeType">
            <a-radio-button value="1"> 根据合同签订人批量更新合同跟进专员 </a-radio-button>
            <a-radio-button value="2"> 根据报工人批量更新参与人 </a-radio-button>
          </a-radio-group>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item :label="form.type == '1' ? '合同签订人' : '合同报工人'">
            <UserInputSelector
                    :username.sync="form.oldUserId"
                    :realname.sync="form.oldUser"
                    :disabled="!checkPermission(['WORK_REPORT_ADMIN','WORK_REPORT_MANAGER'])"
                    multiple="default"
                    placeholder="请选择"
                  />            
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item v-if="form.type == '1'" label="合同跟进专员'">
            <UserInputSelector
                    :username.sync="form.newUserId"
                    :realname.sync="form.newUser"
                    :disabled="!checkPermission(['WORK_REPORT_ADMIN','WORK_REPORT_MANAGER'])"
                    multiple="multiple"
                    placeholder="请选择"
                  />   
          </a-form-model-item>
          <a-form-model-item v-if="form.type == '2'" label="合同报工参与人'">
            <UserInputSelector
                    :username.sync="form.newUserId"
                    :realname.sync="form.newUser"
                    :disabled="!checkPermission(['WORK_REPORT_ADMIN','WORK_REPORT_MANAGER'])"
                    multiple="multiple"
                    placeholder="请选择"
                  />   
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'
export default {
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    permission: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  data() {
    return {
      urls: {
        personList: '/person/info/personList',
        updateTaskUser: '/workReport/project/updateTaskUser',
      },
      form: {type:"1"},
      personList: [],
      personMap: {},
      existPersonMap: {},
      currentType: '',
      mutil: '1',
      selectUserKeyWord: null,
      selectUserTimeOut:null
    }
  },
  mounted() {},
  methods: {
    checkPermission(powers) {
      let hasPower = false
      for (let p of powers) {
        if (this.permission.indexOf(p) > -1) {
          hasPower = true
          break
        }
      }
      return hasPower
    },
    handleOk() {
      postAction(this.urls.updateTaskUser, this.form).then((res) => {
        if (res.success) {
          this.$messageWorkReport.success('更新成功')
          this.$emit('close')
        } else {
          this.$messageWorkReport.warning('更新失败' + res.message)
        }
      })
    },
    handleCancel() {
      this.$emit('close')
    },

    clickPersonSelector(type, mutil) {
      this.currentType = type
      this.mutil = mutil
      let usernames = this.custom[type + 'Id']?this.custom[type + 'Id'].split(","):[]
      let realnames = this.custom[type]?this.custom[type].split(","):[]
      for(let i = 0;i<realnames.length;i++){
        this.$set(this.existPersonMap,realnames[i],usernames[i])
      }
    },    
    changeType() {
      if (this.form.type == '1') {
        this.$set(this.form, 'handlerUser', '')
        this.$set(this.form, 'handlerUserId', '')
      } else if (this.form.type == '2') {
        this.$set(this.form, 'signUser', '')
        this.$set(this.form, 'signUserId', '')
      }
    },
  },
  watch: {
    // projectId(newV, oldV) {
    //   this.fetchProject()
    // },
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
</style>
